﻿// <copyright file="NetworkValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>NetworkValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;

  /// <summary>
  /// A class for validating Network instances.
  /// </summary>
  public class NetworkValidator : AbstractValidator<INetwork>
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="NetworkValidator"/> class.
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply.</param>
    public NetworkValidator(RuleSet ruleSet)
    {
      // Universal rules

      // Subcomponent rules
      RuleForEach(network => network.Areas).SetValidator(new AreaValidator(ruleSet));
      RuleForEach(network => network.CrossSections).SetValidator(new CrossSectionValidator(ruleSet));
      RuleForEach(network => network.Directors).SetValidator(new DirectorValidator(ruleSet));
      RuleForEach(network => network.Links).SetValidator(new LinkValidator(ruleSet));
      RuleForEach(network => network.Nodes).SetValidator(new NodeValidator(ruleSet));
      RuleForEach(network => network.Weirs).SetValidator(new WeirValidator(ruleSet));

      // RuleSet rules
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.LinkCountGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          this.LinkCountGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          // N/A
          break;
      }
    }

    private void LinkCountGreaterThanZero(ErrorLevel level)
    {
      RuleFor(network => network.Links.Count)
        .GreaterThan(0)
        .WithState(network => new ValidationState(network, level))
        .WithMessage("The network must have at least one link.");
    }
  }
}
